package com.weixing.mall.provider.config;

import com.weixing.mall.provider.job.*;
import com.weixing.mall.provider.listener.JobLogsListener;
import org.quartz.*;
import org.springframework.boot.autoconfigure.quartz.SchedulerFactoryBeanCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * @author jzy
 * @date 2020/5/9
 */
@Configuration
public class JobConfig implements SchedulerFactoryBeanCustomizer {


    /**
     * 定时任务同步库存统计表信息--JobDetail
     *
     * @return
     */
    @Bean
    public JobDetail productStockReportJobDetail() {
        return JobBuilder.newJob(ProductStockReportJob.class)
                .withIdentity("productStockReportJobDetail", "productStockReport")
                .storeDurably()
                .build();
    }

    /**
     * 定时任务同步库存统计表信息--调度Trigger  0 0 0 1 * ?  每月1号 00:00:00执行
     *
     * @return
     */
    @Bean
    public Trigger productStockTrigger() {
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(productStockReportJobDetail())
                .withIdentity("productStockTrigger", "productStockTrigger")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0 0 1 * ?"))
                .build();
        return trigger;
    }


    /**
     * 定时任务自动确认待收货订单--JobDetail
     *
     * @return
     */
    @Bean
    public JobDetail autoConfirmOrderJobDetail() {
        return JobBuilder.newJob(OrderAutoConfirmJob.class)
                .withIdentity("autoConfirmOrderJobDetail", "autoConfirmOrderReport")
                .storeDurably()
                .build();
    }

    /**
     * 定时任务自动确认待收货订单--调度Trigger
     *
     * @return
     */
    @Bean
    public Trigger autoConfirmOrderTrigger() {
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(autoConfirmOrderJobDetail())
                .withIdentity("autoConfirmOrderTrigger", "autoConfirmOrderTrigger")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 1 * * * ?"))
                .build();
        return trigger;
    }

    /**
     * 代理商晋级消息通知任务--JobDetail
     */
    @Bean
    public JobDetail agentUpgradeMsgJobDetail() {
        return JobBuilder.newJob(AgentUpgradeMsgJob.class)
                .withIdentity("agentUpgradeMsgJobDetail", "agentUpgradeMsg")
                .storeDurably()
                .build();
    }

    /**
     * 代理商晋级消息通知任务--Trigger
     */
    @Bean
    public Trigger agentUpgradeMsgTrigger() {
        return TriggerBuilder.newTrigger()
                .forJob(agentUpgradeMsgJobDetail())
                .withIdentity("agentUpgradeMsgTrigger", "agentUpgradeMsgTrigger")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 1 * * * ?"))
                .build();
    }


    /**
     * 代理商过期任务--JobDetail
     */
    @Bean
    public JobDetail agentExpireMsgJobDetail() {
        return JobBuilder.newJob(AgentExpireMsgJob.class)
                .withIdentity("agentExpireMsgJobDetail", "agentExpireMsg")
                .storeDurably()
                .build();
    }

    /**
     * 代理商过期任务--Trigger
     */
    @Bean
    public Trigger agentExpireMsgTrigger() {
        return TriggerBuilder.newTrigger()
                .forJob(agentExpireMsgJobDetail())
                .withIdentity("agentExpireMsgTrigger", "agentExpireMsgTrigger")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 1 * * * ?"))
                .build();
    }



    /**
     * 代理商过期任务--JobDetail
     */
    @Bean
    public JobDetail agentAuditJobDetail() {
        return JobBuilder.newJob(AgentAuditJob.class)
                .withIdentity("agentAuditJobDetail", "agentAuditJob")
                .storeDurably()
                .build();
    }

    /**
     * 代理商过期任务--Trigger
     */
    @Bean
    public Trigger agentAuditTrigger() {
        return TriggerBuilder.newTrigger()
                .forJob(agentAuditJobDetail())
                .withIdentity("agentAuditTrigger", "agentAuditTrigger")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0 17 * * ?"))
                .build();
    }
    /**
     * 初始任务监听器
     *
     * @return
     */
    @Bean
    public JobLogsListener jobLogsListener() {
        return new JobLogsListener();
    }

    @Override
    public void customize(SchedulerFactoryBean schedulerFactoryBean) {
        //延时5秒启动
        schedulerFactoryBean.setStartupDelay(5);
        schedulerFactoryBean.setAutoStartup(true);
        schedulerFactoryBean.setOverwriteExistingJobs(true);
        // 任务执行日志监听
        schedulerFactoryBean.setGlobalJobListeners(jobLogsListener());
    }
}
